; randBMP: A random bitmap generator
; Copyright (C) 2010 Erik Weatherwax

; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.

; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>.

section .data
		; BMP header

	MAGIC		dw	0x4d42
	APP_SPEC	dd	0x0
	OFFSET		dd	0x36
	HEAD_SIZE	dd	0x28
	NUM_PLANES	dw	0x2
	BITS_PER_PX	dw	0x18
	COMPR_TYPE	dd	0X0
	HORIZ_RES	dd	0X0B13
	VERT_RES	dd	0X0B13
	COLOR_PAL	dd	0X0
	IMPORTANT_COL	dd	0X0

section .bss
	header		resb	54

section .text

;writeHeader(int fd, int filesize, int datasize, int width, int height)

writeHeader:
	movzx	eax, WORD [MAGIC]
	mov	[header], eax

	mov	eax, [esp + 8]		; get filesize
	mov	[header + 2], eax	; 

	mov	eax, [APP_SPEC] 
	mov	[header + 6], eax

	mov	eax, [OFFSET]
	mov	[header + 10], eax

	mov	eax, [HEAD_SIZE]
	mov	[header + 14], eax

	mov	eax, [esp + 16]		; get width
	mov	[header + 18], eax

	mov	eax, [esp + 20]		; get height
	mov	[header + 22], eax

	movzx	eax, WORD [NUM_PLANES]
	mov	[header + 26], eax

	movzx	eax, WORD [BITS_PER_PX]
	mov	[header + 28], eax

	mov	eax, [COMPR_TYPE]
	mov	[header + 30], eax

	mov	eax, [esp + 12]		; get datasize
	mov	[header + 34], eax

	mov	eax, [HORIZ_RES]
	mov	[header + 38], eax

	mov	eax, [VERT_RES]
	mov	[header + 42], eax

	mov	eax, [COLOR_PAL]
	mov	[header + 46], eax

	mov	eax, [IMPORTANT_COL]
	mov	[header + 50], eax

	mov	eax, 4			; sys_write call
	mov	ebx, [esp + 4]		; file descriptor for created file
	mov	ecx, header		; address of header in RAM
	mov	edx, 54			; header is 54 bytes to write
	int	0x80			; call kernel

	ret	20
